package com.wll.ppjoke.ui.my;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.wll.libcommon.utils.StatusBar;
import com.wll.libnavannotation.FragmentDestination;
import com.wll.ppjoke.R;
import com.wll.ppjoke.databinding.FragmentMyBinding;
import com.wll.ppjoke.model.User;
import com.wll.ppjoke.ui.login.UserManager;
import com.wll.ppjoke.view.PPImageView;

@FragmentDestination(pageUrl = "main/tabs/my" /*, needLogin = true*/)
public class MyFragment extends Fragment {
    private FragmentMyBinding mBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = FragmentMyBinding.inflate(inflater, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        User user = UserManager.get().getUser();
        mBinding.setUser(user);

        UserManager.get().refresh().observe(getViewLifecycleOwner(), newUser -> {
            if (newUser != null) {
                mBinding.setUser(newUser);
            }
        });
        if (mBinding.getUser() == null){
            Log.e("MyFragment","user null");
//            PPImageView.setImageUrl(mBinding.avatar, null,true,R.mipmap.placehoder);
//            PPImageView.setBlurUrl(mBinding.blurBackground, null,0,R.mipmap.placehoder);
//            mBinding.blurBackground.setBackgroundColor(getResources().getColor(R.color.color_999));
            mBinding.blurBackground.setBackground(getResources().getDrawable(R.mipmap.placehoder));
        }

        mBinding.actionLogout.setOnClickListener(v -> new AlertDialog.Builder(getContext())
                .setMessage(getString(R.string.fragment_my_logout))
                .setPositiveButton(getString(R.string.fragment_my_logout_ok), (dialog, which) -> {
                    dialog.dismiss();
                    UserManager.get().logout();
                    getActivity().onBackPressed();
                }).setNegativeButton(getString(R.string.fragment_my_logout_cancel), null)
                .create().show());

        mBinding.goDetail.setOnClickListener(v ->
                ProfileActivity.startProfileActivity(getContext(), ProfileActivity.TAB_TYPE_ALL));
        mBinding.userFeed.setOnClickListener(v ->
                ProfileActivity.startProfileActivity(getContext(), ProfileActivity.TAB_TYPE_FEED));
        mBinding.userComment.setOnClickListener(v ->
                ProfileActivity.startProfileActivity(getContext(), ProfileActivity.TAB_TYPE_COMMENT));
        mBinding.userFavorite.setOnClickListener(v ->
                UserBehaviorListActivity.startBehaviorListActivity(getContext(), UserBehaviorListActivity.BEHAVIOR_FAVORITE));
        mBinding.userHistory.setOnClickListener(v ->
                UserBehaviorListActivity.startBehaviorListActivity(getContext(), UserBehaviorListActivity.BEHAVIOR_HISTORY));
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBar.lightStatusBar(getActivity(), false);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        StatusBar.lightStatusBar(getActivity(), hidden);
    }

//    private static final String TAG = "MyFragment";
//    private MyViewModel myViewModel;
//
//    public View onCreateView(@NonNull LayoutInflater inflater,
//                             ViewGroup container, Bundle savedInstanceState) {
//        Log.e(TAG,"onCreateView");
//
//        myViewModel =
//                ViewModelProviders.of(this).get(MyViewModel.class);
//        View root = inflater.inflate(R.layout.fragment_my, container, false);
//        final TextView textView = root.findViewById(R.id.text_home);
//        myViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                textView.setText(s);
//            }
//        });
//        return root;
//    }
}
